<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class MyModel extends CI_Model{

    public function add($dbTable='',array $data = null){
        if(!$dbTable){
            $dbTable = $this->dbTable;
        }
        if(!$data){
            return FALSE;
        }else{
            $i=0;
            foreach($data as $val){
                if(is_array($val)){
                    $i=1;
                }
            }
            if($i){
                return $this->db->insert_batch($dbTable,$data);
            }else{
                $this->db->insert($dbTable,$data);
                return $this->db->insert_id();
            }
        }

    }

    public function update($dbTable='',array $data = null){
        if(!$dbTable){
            $dbTable = $this->dbTable;
        }
        if(!$data){
            return FALSE;
        }else{
            $i=0;
            foreach($data as $val){
                if(is_array($val)){
                    $i=1;
                }
            }
            if($i){
                return $this->db->update_batch($dbTable,$data);
            }else{
                return $this->db->replace($dbTable,$data);
            }

        }

    }
    public function delete($dbTable='',array $condition = null){
        if(!$dbTable){
            $dbTable = $this->dbTable;
        }
        if(!$condition){
            return $this->db->empty_table($dbTable);
        }else{
            $i=0;
            $feild ='';
            $value = array();
            foreach($condition as $key=>$val){
                if(is_array($val)){
                    $i=1;
                    $feild = $key;
                    $value = $val;
                }
            }
            if($i){
                return $this->db->where_in($feild,$value)->delete($dbTable);
            }else{
                return $this->db->delete($dbTable,$condition);
            }

        }

    }

    public function select($dbTable='',$data = '',$join=array(),$condition=array(),$order='',$limit=array()){
        if(!$dbTable){
            $dbTable = $this->dbTable;
        }
        if(!$data){
            $data = '*';
        }
        $this->db->select($data)->from($dbTable);
        if($join){
            $i=0;
            foreach($join as $val){
                if(is_array($val)){
                    $i=1;
                }
            }
            if($i){
                for($j=0; $j<count($join); $j++){
                    $this->db->join($join[$j][0],$dbTable.'.'.$join[$j][1].'='.$join[$j][0].'.'.$join[$j][1],$join[$j][2]);
                }
            }else{
                $this->db->join($join[0],$dbTable.'.'.$join[1].'='.$join[0].'.'.$join[1],$join[2]);
            }

        }
        if($condition){
            $i=0;
            $feild ='';
            $value = null;
            foreach($condition as $key=>$val){
                if(is_array($val) && !empty($val)){
                    $i=1;
                    $feild = $key;
                    $value = $val;
                }
            }
            if($i && $value){
                $this->db->where_in($feild,$value);
            }else{
                $this->db->where($condition);
            }
        }

        if($order){
            $this->db->order_by($order);
        }

        if($limit){
            $this->db->limit($limit[0],$limit[1]);
        }
        $query = $this->db->get();
        $array=array();
        if($query){
            if(!isset($limit[2])){
                $array = $query->result_array();
            }else{
                $array = $query->row_array();
            }

        }
        return $array;
    }


    public function getCount($condition=array()){
        if($condition){
            $i=0;
            $feild ='';
            $value = array();
            foreach($condition as $key=>$val){
                if(is_array($val)){
                    $i=1;
                    $feild = $key;
                    $value = $val;
                }
            }
            if($i){
                $this->db->where_in($feild,$value);
            }else{
                $this->db->where($condition);
            }
        }
        return $this->db->count_all_results($this->dbTable);
    }

}